<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="当前审批人" prop="procName">
        <SelectUserInput v-model="queryParams.userId" placeholder="请选择当前审批人" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="showHandover(null)"
        >批量转交</el-button>
      </el-col>
    </el-row>
    <el-table ref="dataListRef" v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="审批编号" align="center" prop="procInstId" />
      <el-table-column label="标题" align="center" prop="procName" />
      <el-table-column label="流程状态" align="center" prop="approvalStatus" >
        <template #default="scope">
          <dict-tag :options="bpm_approval_status" :value="scope.row.approvalStatus" />
        </template>
      </el-table-column>
      <el-table-column label="提交时间" align="center" prop="submitTime" width="180" />
      <el-table-column label="完成时间" align="center" prop="finishTime" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="showProcInstInfo(scope.row.procInstId)">详情</el-button>
          <el-button link type="primary" icon="Edit" @click="showHandover(scope.row)">转交</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 退回 -->
    <el-dialog v-model="handoverDialog" title="转交审批单" width="500">

      <h3>接收审批流程的人（交接人）</h3>
      <SelectUserInput v-model="toUserId" placeholder="请选择" />

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handoverDialog = false">取消</el-button>
          <el-button type="primary" @click="doHandover">确定</el-button>
        </div>
      </template>
    </el-dialog>

    <ProcInstInfo ref="procInstInfoRef" @close="closeProcInstInfo"/>
  </div>
</template>

<script setup>
import ProcInstInfo from "../components/ProcInstInfo.vue";
import {handoverList, handover, getRollbackNodes} from "@/api/bpm/approval";
import {getCurrentInstance, nextTick, onMounted, reactive, ref, toRefs, watch} from "vue";
import SelectUserInput from "@/components/SelectUserInput/index.vue";

const { proxy } = getCurrentInstance();

const { bpm_approval_status } = proxy.useDict("bpm_approval_status");
const dataList = ref([]);
const loading = ref(false);
const showSearch = ref(true);
const toUserId = ref(null);
const queryParams = ref({});
const procInstInfoRef = ref(null);
const dataListRef = ref(null);
const handoverDialog = ref(false);
const selectionIds = ref([]);

/** 查询流程交接列表 */
function getList() {
  if (queryParams.value.userId == null || queryParams.value.userId === '') {
    proxy.$modal.msgError("请选择当前审批人")
    return
  }
  loading.value = true;
  handoverList(queryParams.value).then(response => {
    dataList.value = response.rows;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.userId = null
  dataList.value = []
}

function showProcInstInfo(procInstId) {
  procInstInfoRef.value.show(procInstId)
}
function closeProcInstInfo() {
  getList()
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  selectionIds.value = selection.map(item => item.handleRecordId);

}

function showHandover(row) {
  if (row != null) {
    dataListRef.value.clearSelection()
    dataListRef.value.toggleRowSelection(row, true)
  }
  if (selectionIds.value == null || selectionIds.value.length === 0) {
    proxy.$modal.msgError("请选择审批单")
    return
  }

  toUserId.value = null
  handoverDialog.value = true
}
function doHandover() {
  let data = {
    toUserId: toUserId.value,
    recordIds: selectionIds.value
  }
  handover(data).then(response => {
    proxy.$modal.msgSuccess("转交成功");
    handoverDialog.value = false
    getList()
  });
}
</script>

<style scoped lang="scss">

</style>